package controller;

import gui.Frame;
import model.UserInterface;
import rmi.client.ClientRemote;
import rmi.server.Message;
import rmi.server.Server;
import rmi.server.User;
import toolkit.Debug;
import toolkit.Parse;
import view.View;

abstract public class Controller
{
	public static UserInterface user;
	
	public static ClientRemote client;
	
	protected Server serverRemote;
	
	protected User userRemote;
	
	protected Message messageRemote;
	
	protected View view;

	protected Class previousController;
	
	protected static Controller lastOpened;
	
	public static Controller getLastOpened()
	{
		return lastOpened;
	}
	
	public Controller()
	{
		Controller.lastOpened = this;
	}
	
	public void setUser(UserInterface user)
	{
		this.user = user;
	}
	
	public UserInterface getUser()
	{
		return user;
	}

	public void setClient(ClientRemote client)
	{
		this.client = client;
	}
	
	public ClientRemote getClient()
	{
		return client;
	}

	public void setServerRemote(Server pokerRemote)
	{
		this.serverRemote = pokerRemote;
	}
	
	public Server getServerRemote()
	{
		return serverRemote;
	}

	public void setUserRemote(User userRemote)
	{
		this.userRemote = userRemote;
	}
	
	public User getUserRemote()
	{
		return userRemote;
	}

	public void setMessageRemote(Message messageRemote)
	{
		this.messageRemote = messageRemote;
	}
	
	public Message getMessageRemote()
	{
		return messageRemote;
	}
	
	public void setView(View view)
	{
		this.view = view;
	}
	
	public View getView()
	{
		return view;
	}
	
	public void forward(Controller controller)
	{
		forward(controller, false);
	}
	
	public void forward(Controller controller, boolean newFrame)
	{
		controller.copy(this);
		
		controller.show(newFrame);

		controller.dispatch();
	}
	
	public void goBack()
	{
		try
		{
			forward((Controller) previousController.newInstance());
		}
		catch (Exception e)
		{
			Debug.log(e);
		}
	}
	
	public void copy(Controller controller)
	{
		previousController = controller.getClass();
		
		setUser(controller.getUser());
		
		setServerRemote(controller.getServerRemote());
		setUserRemote(controller.getUserRemote());
		setMessageRemote(controller.getMessageRemote());
	}
	
	public void dispatch()
	{
		
	}
	
	public void show()
	{
		show(false);
	}
	
	public void show(boolean newFrame)
	{
		Frame frame;
		
		if (newFrame || Frame.getLastOpened() == null)
		{
			frame = new Frame();
		}
		else
		{
			frame = Frame.getLastOpened();
			frame.setVisible(false);
			frame.getContentPane().removeAll();
		}
		
		frame.setResizable(false);
		
		frame.getContentPane().add(view);
		frame.setTitle(Parse.toNonCamelCase(view.getClass().getSimpleName()));
		frame.getRootPane().setDefaultButton(view.getDefaultButton());
		
		view.setFrame(frame);
		
		view.postInitGUI();
		
		frame.pack();
		frame.repaint();
		frame.setVisible(true);
		
		frame.center();
	}
}
